package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public PageResult pageQuery (QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //pageHelper
        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Permission> rows = page.getResult();
        return new PageResult(total,rows );
    }

    @Override
    public void add (Permission permission, Integer[] menuIds) {
        permissionDao.add(permission);
        //设置菜单和权限多对多的关联关系
        Integer permissionId = permission.getId();
        this.setMenuAndPermission(permissionId, menuIds);
    }

    @Override
    public void edit (Permission permission, Integer[] menuIds) {
        //修改权限基本信息，操作权限t_permission表
        permissionDao.edit(permission);
        //清理当前权限关联的菜单，擦偶作中间关系表t_menu_permission表
        permissionDao.deleteAssocication(permission.getId());
        //重新建立当前权限和菜单的关联关系
        Integer permissionId = permission.getId();
        this.setMenuAndPermission(permissionId,menuIds);
    }

    @Override
    public Permission findById (Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public void deleteById (Integer id) {
        //清理当前权限关联的菜单，中间关系表t_role_permission表
        permissionDao.deleteAssocication(id);
        permissionDao.deleteById(id);
    }

    @Override
    public List<Permission> findAll () {
        return permissionDao.findAll();
    }

    @Override
    public List<Menu> findMenuIdsByPermissionId (Integer id) {
        return permissionDao.findMenuIdsByPermissionId(id);
    }

    @Override
    public List<Integer> findpermissionidsbyroleid(Integer roleId) {
        return permissionDao.findpermissionidsbyroleid(roleId);
    }


    //建立权限和菜单多对多关系
    public void setMenuAndPermission (Integer permissionId, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("permissionId", permissionId);
                map.put("menuId", menuId);
                permissionDao.setMenuAndPermission(map);
            }
        }
    }
}
